﻿<?xml version="1.0" encoding="utf-8"?>
<Database xmlns="Moregen.Database" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Moregen.Database database.xsd">
	<FixCase>false</FixCase>
	<Schema>
		<TableSQL>
      SELECT o.name, case when xtype='U' then 0 else 1 end IsView,
      (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES where TABLE_NAME=o.name) as SchemaName,
      case when (exists (select *
      FROM      INFORMATION_SCHEMA.VIEWS
      WHERE     TABLE_NAME = o.name and IS_UPDATABLE= 'YES')
      OR
      exists (select *
      FROM      INFORMATION_SCHEMA.TABLES
      WHERE     TABLE_NAME = o.name and TABLE_TYPE = 'BASE TABLE') )
      then 1 else 0 end
      as IsUpdateable
      FROM sysobjects o
      WHERE
      xtype in ('U', 'V')
      AND objectproperty(o.id, 'IsMsShipped')=0
      and o.name not in ('fn_diagramobjects', 'sp_alterdiagram', 'sp_creatediagram', 'sp_dropdiagram', 'sp_helpdiagramdefinition', 'sp_helpdiagrams', 'sp_renamediagram', 'sp_upgraddiagrams', 'sysdiagrams')
    </TableSQL>
		<ColSQL>
      SELECT
      (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES where TABLE_NAME=T.name) as SchemaName,
      (SELECT  SubString(COLUMN_DEFAULT, 2, LEN(COLUMN_DEFAULT)-2)
      FROM      INFORMATION_SCHEMA.COLUMNS
      WHERE     TABLE_NAME = T.name
      AND       COLUMN_NAME = C.name) as DefaultValue,
      (select 	1
      from 	INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
      INFORMATION_SCHEMA.KEY_COLUMN_USAGE cc
      where 	pk.TABLE_NAME = T.name
      and	CONSTRAINT_TYPE = 'UNIQUE'
      and cc.COLUMN_NAME = C.name
      and	cc.TABLE_NAME = pk.TABLE_NAME
      and	cc.CONSTRAINT_NAME = pk.CONSTRAINT_NAME) as IsUnique,
      T.name TableName, C.name ColumnName,
      ST.name ColumnType,
      C.prec, C.scale, C.isnullable, C.iscomputed,
      ColumnProperty(C.id, C.name, 'IsIdentity') IsIdentity
      FROM syscolumns C INNER JOIN sysobjects T ON C.id = T.id
      INNER JOIN systypes ST ON C.xtype = ST.xtype
      where (T.xtype='U' or T.xtype='V')
      and ST.status !=1 and ST.uid !=1
      order by TableName
    </ColSQL>
		<PKSQL>
      select distinct
      tbl.name TableName,
      (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES where TABLE_NAME=tbl.name) as SchemaName,
      col.name ColName,
      constrId.name PkName,
      sik.keyno,
      case when ix.indid = 1 then 1 else 0 end IsClustered
      from sysobjects tbl
      join sysconstraints constr on ( tbl.id = constr.id and tbl.xtype = 'U' and constr.status &amp; 0x0001 = 0x0001 )
			join sysobjects constrId on constrId.parent_obj = tbl.id and constrId.xtype = 'PK'
			join sysindexes ix on constrId.name = ix.name and ix.id = tbl.id
			join sysindexkeys sik on sik.id = tbl.id and sik.indid = ix.indid
			join syscolumns col on col.id = tbl.id and sik.colid = col.colid
			ORDER BY tbl.name, constrId.name, sik.keyno
		</PKSQL>
		<FKSQL>
      SELECT cast(f.name AS varchar(255)) AS fk_name,
      (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES where TABLE_NAME=ft.name) as fSchemaName,
      r.keycnt,
      cast(ft.name AS varchar(255)) AS foreign_table,
      cast(f1.name AS varchar(255)) AS foreign_col1,
      cast(pt.name AS varchar(255)) AS primary_table,
      (select TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES where TABLE_NAME=pt.name) as pSchemaName,
      cast(p1.name AS varchar(255)) AS primary_col1
      FROM sysobjects f
      INNER JOIN sysobjects ft ON f.parent_obj = ft.id
      INNER JOIN sysreferences r ON f.id = r.constid
      INNER JOIN sysobjects pt ON r.rkeyid = pt.id
      INNER JOIN syscolumns p1 ON r.rkeyid = p1.id AND r.rkey1 = p1.colid
      INNER JOIN syscolumns f1 ON r.fkeyid = f1.id AND r.fkey1 = f1.colid
      WHERE f.type = 'F'
      order by primary_table, foreign_table
    </FKSQL>
	</Schema>
	<Types>
		<DBType>bigint</DBType>
		<csType>long</csType>
		<csType>ulong</csType>
	</Types>
	<Types>
		<DBType>binary</DBType>
		<csType>byte[]</csType>
	</Types>
	<Types>
		<DBType>bit</DBType>
		<csType>bool</csType>
	</Types>
	<Types>
		<DBType>char</DBType>
		<csType>string</csType>
		<csType>char</csType>
		<csType>byte</csType>
	</Types>
  <Types>
    <DBType>datetime</DBType>
    <csType>DateTime</csType>
  </Types>
  <Types>
    <DBType>date</DBType>
    <csType>DateTime</csType>
  </Types>
  <Types>
		<DBType>decimal</DBType>
		<csType>decimal</csType>
		<csType>float</csType>
		<csType>double</csType>
		<csType>int</csType>
		<csType>long</csType>
	</Types>
	<Types>
		<DBType>float</DBType>
		<csType>double</csType>
		<csType>decimal</csType>
		<csType>float</csType>
	</Types>
	<Types>
		<DBType>image</DBType>
		<csType>byte[]</csType>
	</Types>
	<Types>
		<DBType>int</DBType>
		<csType>int</csType>
		<csType>long</csType>
		<csType>uint</csType>
		<csType>ulong</csType>
	</Types>
	<Types>
		<DBType>money</DBType>
		<csType>decimal</csType>
		<csType>float</csType>
		<csType>double</csType>
	</Types>
	<Types>
		<DBType>nchar</DBType>
		<csType>string</csType>
		<csType>char</csType>
		<csType>byte</csType>
	</Types>
	<Types>
		<DBType>ntext</DBType>
		<csType>string</csType>
	</Types>
	<Types>
		<DBType>numeric</DBType>
		<csType>decimal</csType>
		<csType>float</csType>
		<csType>double</csType>
		<csType>int</csType>
		<csType>long</csType>
	</Types>
	<Types>
		<DBType>nvarchar</DBType>
		<csType>string</csType>
	</Types>
	<Types>
		<DBType>real</DBType>
		<csType>float</csType>
		<csType>decimal</csType>
		<csType>double</csType>
	</Types>
	<Types>
		<DBType>smalldatetime</DBType>
		<csType>DateTime</csType>
	</Types>
	<Types>
    <DBType>smallint</DBType>
    <csType>short</csType>
		<csType>Int16</csType>
		<csType>int</csType>
		<csType>long</csType>
		<csType>uint</csType>
		<csType>ulong</csType>
	</Types>
	<Types>
		<DBType>smallmoney</DBType>
		<csType>decimal</csType>
		<csType>float</csType>
		<csType>double</csType>
	</Types>
	<Types>
		<DBType>sql_variant</DBType>
		<csType>object</csType>
	</Types>
	<Types>
		<DBType>sysname</DBType>
		<csType>string</csType>
	</Types>
	<Types>
		<DBType>text</DBType>
		<csType>string</csType>
	</Types>
	<Types>
		<DBType>timestamp</DBType>
		<csType>long</csType>
		<csType>ulong</csType>
	</Types>
	<Types>
		<DBType>tinyint</DBType>
		<csType>byte</csType>
		<csType>sbyte</csType>
		<csType>short</csType>
		<csType>Int16</csType>
		<csType>int</csType>
		<csType>long</csType>
		<csType>Int32</csType>
		<csType>uint</csType>
		<csType>ulong</csType>
	</Types>
	<Types>
		<DBType>uniqueidentifier</DBType>
    <csType>Guid</csType>
    <csType>string</csType>
    <csType>byte[]</csType>
	</Types>
	<Types>
		<DBType>varbinary</DBType>
		<csType>byte[]</csType>
	</Types>
  <Types>
    <DBType>varchar</DBType>
    <csType>string</csType>
  </Types>
  <Types>
    <DBType>xml</DBType>
    <csType>string</csType>
  </Types>
</Database>